package dongze.com.ckdj.service.uploader;

import android.text.TextUtils;

import com.google.gson.JsonObject;

import dongze.com.ckdj.data.DataLayer;
import dongze.com.ckdj.map.MapUtil;
import dongze.com.ckdj.persistence.dao.DaoSession;
import dongze.com.ckdj.persistence.dao.MartinImageCheckInRecord;
import dongze.com.ckdj.persistence.dao.MartinImageCheckInRecordDao;
import dongze.com.ckdj.persistence.dao.ServerUploadTask;
import io.reactivex.ObservableEmitter;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;


/**
 * Created by fanqwz on 2017/12/21.
 */

public class MartinImageCheckInUploader extends Uploader<String, String> {
    private static final String TAG = MartinImageCheckInUploader.class.getSimpleName();
    private ServerUploadTask task;
    MapUtil mapUtil;
    String typeName;
    Integer imageIdex;
    String userNo;
    String vin;

    public MartinImageCheckInUploader(Scheduler subscribeThread, Scheduler observerThread, ServerUploadTask task, MapUtil mapUtil, DataLayer dataLayer, DaoSession session, ImageUploader.ImageProgressListener listener) {
        super(AndroidSchedulers.mainThread(), observerThread);
        MartinImageCheckInRecord record = session.getMartinImageCheckInRecordDao().queryBuilder().where(MartinImageCheckInRecordDao.Properties.Id.eq(task.getRecordId())).uniqueOrThrow();
        this.task = task;
        this.mapUtil = mapUtil;
        this.typeName = record.getTypeName();
        this.imageIdex = record.getImageIndex();
        this.userNo = record.getUser();
        this.vin = record.getVin();

        dependsOn(new ImageUploader(subscribeThread,
                observerThread,
                dataLayer,
                session.getImageDao(),
                task.getImage(),
                listener));
    }

    @Override
    Worker<String, String> getWorker() {
        return worker;
    }

    Worker<String, String> worker = new Worker<String, String>() {
        String imageRet;

        @Override
        public void accept(String depends) {
            imageRet = depends;
        }

        @Override
        public void doWork(final ObservableEmitter<String> e) {
            if (imageRet == null) {
                e.onError(new Exception("upload mass loss but image return null"));
                return;
            }

//            mapUtil.updateMassLossImageRecord(typeName, imageIdex, imageRet, userNo, new DisposableObserver<String>() {
//                @Override
//                public void onNext(String value) {
//                    if (value != null) {
//                        MyLog.d(TAG, "onNext: " + typeName + ":" + value);
//                        e.onNext(value);
//                    } else {
//                        e.onError(new Exception(MartinImageCheckInUploader.class.getSimpleName() + ":do work failed"));
//                    }
//                }
//
//                @Override
//                public void onError(Throwable ex) {
//                    MyLog.e(TAG, "onError: " + typeName + "[" + imageIdex + ":" + imageRet + "]");
//                    e.onError(ex);
//                }
//
//                @Override
//                public void onComplete() {
//                    MyLog.d(TAG, "onComplete: " + typeName + "[" + imageIdex + ":" + imageRet + "]");
//                    e.onComplete();
//                }
//            });
            JsonObject jb = new JsonObject();
            jb.addProperty("vin", vin);
            jb.addProperty("img", imageRet);
            jb.addProperty("type", typeName);
            jb.addProperty("user_no", userNo);
            jb.addProperty("index", String.valueOf(imageIdex));
            mapUtil.martinCommitCheckInImage(jb, new DisposableObserver<String>() {
                @Override
                public void onNext(String value) {
                    if (TextUtils.isEmpty(value))
                        e.onError(new Exception("ret null"));
                    else
                        e.onNext(value);
                }

                @Override
                public void onError(Throwable e1) {
                    e.onError(e1);
                }

                @Override
                public void onComplete() {
                    e.onComplete();
                }
            });
        }
    };
}
